package com.baosight.sczx.zm.cc.service;

import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.common.constant.EPResource;
import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cc.OperationHistory;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblemModify;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblemNeedModify_x;

/**
 * 
 * <p>Title: ServiceZMCC20.java</p>
 * <p>Description: 生产问题整改</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2016年9月28日 下午1:40:31  郭淑芳  Create
 *</pre>
 */
public class ServiceZMCC20 extends ServiceEPBase {
	private static Dao dao = (Dao) SpringApplicationContext.getBean("dao");
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCPB0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-8-10上午02:36:19 严洪浩
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的

		this.ModifyByReceive();
		
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: 查询方法</p>
	 * <p>return value: ServiceZMCC20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日下午1:37:55 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo) {
		String miningCode = UserSession.getInSessionProperty("miningCode").toString();
		StaticTools.setQueryParameters(inInfo, "fdMiningId", miningCode, true);
		StaticTools.setQueryParameters(inInfo, "fdBusinessDepartmentId", UserSession.getInSessionProperty("orgCode"), true);// 隐患部门
		StaticTools.setQueryParameters(inInfo, "isModifying", "yes", true);// 所有未整改问题
		EiInfo outInfo = super.query(inInfo, "ZMCCProduceproblemNeedModify_x.query", new ZMCCProduceproblemNeedModify_x());

		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemType", false));// 问题类型
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemLevel", false));// 问题级别
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemOrigin"));// 问题来源
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.problemStatus", new String[]{"0", "1", "2", "3", "4", "5", "6"}, new String[]{"草拟", "未处理", "未查收", "未整改", "未核查", "关闭", "作废"}, true));//问题状态
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.isCheck", new String[] { "否", "是" }, new String[] { "否", "是" }, true));// 是否重复问题
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false, UserSession.getInSessionProperty("miningCode").toString()));

		return outInfo;
	}
	/**
	 * 
	 * <p>Function: feedback</p>
	 * <p>Description: 反馈 方法</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日下午1:38:08 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo feedback(EiInfo inInfo) {
		String fdRecordId = inInfo.getBlock("result").getCellStr(0, "fdRecordId");// 生产问题记录ID
		int count = this.ModiyReback(fdRecordId);// 修改隐患记录状态

		if (count > 0) {
			String fdKeyId = TablePk.getInstance().getNextValue("T_CC_PRODUCEPROBLEM_MODIFY");// 生产问题记录ID
			inInfo.getBlock("result").setCell(0, "fdKeyId", fdKeyId);// ID
			inInfo.getBlock("result").setCell(0, "fdCreateTime", StaticTools.getDateTime());// 创建时间

			EiInfo outInfo = super.insert(inInfo, "ZMCCProduceproblemModify.insert");

			inInfo.setCell("result", 0, "fdKeyId", fdRecordId);
			boolean insertValue = OperationHistory.InsertOperationHistory(inInfo, "生产问题", "反馈");// 新增隐患处理记录
			if (!insertValue) {
				inInfo.setStatus(-1);
				inInfo.setMsg("插入操作记录失败！");
			}
			outInfo = this.query(inInfo);
			outInfo.setStatus(1);
			outInfo.setMsg("反馈成功！");
			return outInfo;
		} else {
			inInfo.setStatus(-1);
			inInfo.setMsg("该生产问题已经处理，请刷新纪录后操作！");
			return inInfo;
		}
	}
	
	/**
	 * 
	 * <p>Function: getDetailOfProblem</p>
	 * <p>Description: 查询主表中的生产问题对应明细</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日下午1:38:56 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo getDetailOfProblem(EiInfo inInfo) {

		EiInfo outInfo = new EiInfo();
		// 在输出EiInfo中添加明细区对应的block块

		EiBlock eiBlock = outInfo.addBlock("detail");
		ZMCCProduceproblemModify _dom = new ZMCCProduceproblemModify();
		eiBlock.setBlockMeta(_dom.eiMetadata);

		String fdRecordId = inInfo.getString("fdRecordId");
		String fdRecordHandleId = inInfo.getString("fdRecordHandleId");

		eiBlock.setCell(0, "fdRecordId", fdRecordId);
		eiBlock.setCell(0, "fdRecordHandleId", fdRecordHandleId);
		eiBlock.setCell(0, "fdFeedbackPeopleId", UserSession.getUserId().toString());// 反馈人ID
		eiBlock.setCell(0, "fdFeedbackPeople", UserSession.getInSessionProperty("UserCname").toString());// 反馈人
		eiBlock.setCell(0, "fdFeedbackDate", StaticTools.getDate());

		inInfo.setMsgByKey(EPResource.EP_1000, new String[] { String.valueOf(1), "查询明细" });

		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: ModifyByReceive</p>
	 * <p>Description: 责任单位在进入整改模块后，改变生产问题状态为 已查收</p>
	 * <p>return value:int</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日下午1:39:17 郭淑芳
	 * </p>
	 *
	 */
	private int ModifyByReceive() {

		int count = 0;
		if (null == UserSession.getInSessionProperty("orgCode")) {
			return count;
		}

		try {
			String fdDepartmentId = UserSession.getInSessionProperty("orgCode").toString();
			Map<String, String> _argMap = new HashMap<String, String>();
			_argMap.put("fdDepartmentId", fdDepartmentId);

			count = dao.update("ZMCCProduceproblem.modifyByReceive", _argMap);
		} catch (Exception e) {
			System.out.println("修改状态 为已查收异常：" + e.getMessage());
		}
		return count;
	}
	/**
	 * 
	 * <p>Function: ModiyReback</p>
	 * <p>Description: 生产问题 整改后的反馈</p>
	 * <p>return value:int</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日下午1:39:51 郭淑芳
	 * </p>
	 *
	 */
	private int ModiyReback(String fdKeyId) {
		int count = 0;

		try {
			Map<String, String> _argMap = new HashMap<String, String>();
			_argMap.put("fdKeyId", fdKeyId);

			count = dao.update("ZMCCProduceproblem.modifyReback", _argMap);
		} catch (Exception e) {
			System.out.println("隐患整改后的反馈修改状态 为已查收异常：" + e.getMessage());
		}
		return count;
	}
}
